<?php

namespace app\controller;

use app\queue\defaults\DefaultConsumer;
use app\queue\others\DemoConsumer;
use app\support\Printer;
use support\Log;
use support\Request;
use support\Response;
use Webman\Event\Event;
use Webman\Route;
use Xin\EasyQueue\Queue;

class IndexController
{
    static $readme;

    /**
     * 默认接口
     * @param Request $request
     * @return Response
     */
    public function index(Request $request): Response
    {
        DefaultConsumer::dispatch();
        Queue::dispatch(null);
        return success(env('APP_NAME', config('server.name')), [
            'routes' => array_map(function (Route $route) {
                return [
                    'method' => $route->getMethods(),
                    'path' => $route->getPath(),
                ];
            }, Route::getRoutes()),
        ]);
    }

    /**
     * README
     * @return false|string
     */
    public function readme()
    {
        if (!self::$readme || rand(0, 1)) {
            self::$readme = file_get_contents(base_path('README.md'));
        }

        return self::$readme;
    }

    /**
     * View Demo
     * @param Request $request
     */
    public function view(Request $request)
    {
        dump("hello");
        Event::dispatch("demo", null);
    }

    public function testCallback(Request $request)
    {
        $data = $request->post();
        Log::info("任务回调：", $data);

        return json($data);
    }

}
